package yicheng.dinnertogetherapp;

import java.util.Calendar;
import yicheng.dinnertogetherapp.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class AvailabilityActivity extends Activity{
	Button setStartTimeButton, setEndTimeButton;
	TextView startTimeTextView, endTimeTextView;
	
	
	AmazonClientManager clientManager;
	
	SharedPreferences local_user_information;
	SharedPreferences.Editor local_user_editor;
	String PREFS_NAME = "LocalUserInfo";
	
	String PHONE_NUMBER, START_TIME, END_TIME;
	
	int start_time_hour;
	int start_time_min;
	
	int end_time_hour;
	int end_time_min;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		clientManager = new AmazonClientManager();
		setContentView(R.layout.availability_view);
		overridePendingTransition(R.anim.activity_left_in,R.anim.activity_left_out);
		
		setStartTimeButton = (Button) findViewById(R.id.setStartTime_button);
		setEndTimeButton = (Button) findViewById(R.id.setEndTime_button);
		
		startTimeTextView = (TextView) findViewById(R.id.startTime_textView);
		endTimeTextView = (TextView) findViewById(R.id.endTime_textView);
		
		local_user_information =  this.getSharedPreferences(PREFS_NAME,0);
		
		updateAvailablilityDisplay();
		timeToHourAndMin();
		
		setStartTimeButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				START_TIME = startTimeTextView.getText().toString();
				END_TIME = endTimeTextView.getText().toString();
				timeToHourAndMin();
				
				startTimeTextView.setText(START_TIME);
				endTimeTextView.setText(END_TIME);
				/*start_time_hour = 0;
				start_time_min = 0;*/

				TimePickerDialog.OnTimeSetListener dateSetListener = new TimePickerDialog.OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						// TODO Auto-generated method stub
						int hour = hourOfDay;
						int min = minute;
						String AMorPM = (hour <= 12) ?  "AM" :  "PM";
						hour = (hour <= 12) ? hour : hour -12;
						String mins = (min/10 == 0) ? "0" + min : "" + min;
						
						startTimeTextView.setText(hour +" : " +mins + " " + AMorPM);
					}

					
				};


				TimePickerDialog timePickerDialog = new TimePickerDialog(AvailabilityActivity.this, dateSetListener, start_time_hour, start_time_min, true);

				timePickerDialog.show();

			}
		});

		
		
		setEndTimeButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				START_TIME = startTimeTextView.getText().toString();
				END_TIME = endTimeTextView.getText().toString();
				timeToHourAndMin();
				startTimeTextView.setText(START_TIME);
				endTimeTextView.setText(END_TIME);
				/*end_time_hour =0;
				end_time_min = 0;*/
				
				TimePickerDialog.OnTimeSetListener dateSetListener = new TimePickerDialog.OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						// TODO Auto-generated method stub
						int hour = hourOfDay;
						int min = minute;
						String AMorPM = (hour <= 12) ?  "AM" :  "PM";
						hour = (hour <= 12) ? hour : hour -12;
						String mins = (min/10 == 0) ? "0" + min : "" + min;
						
						endTimeTextView.setText(hour +" : " +mins + " " + AMorPM);
						
					}

					
				};


				TimePickerDialog timePickerDialog = new TimePickerDialog(AvailabilityActivity.this, dateSetListener, end_time_hour, end_time_min, true);

				timePickerDialog.show();

			}
		});
		
		
		
	}
	
	
	
	private void updateAvailablilityDisplay(){
		START_TIME = local_user_information.getString("start_time", "0 : 00 AM");
		END_TIME = local_user_information.getString("end_time", "0 : 00 PM");
		startTimeTextView.setText(START_TIME);
		endTimeTextView.setText(END_TIME);
	}
	
	private void updateAvailability(){
		PHONE_NUMBER = local_user_information.getString("PhoneNumber", "default");
		runOnUiThread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Thread(new Runnable(){

					@Override
					public void run() {
						// TODO Auto-generated method stub
						SimpleDB.updateSingleAttribute(clientManager, PHONE_NUMBER,SimpleDB.AVAILABILITY_ITEM, SimpleDB.START_TIME_ATTRIBUTE, startTimeTextView.getText().toString());
						SimpleDB.updateSingleAttribute(clientManager, PHONE_NUMBER,SimpleDB.AVAILABILITY_ITEM, SimpleDB.END_TIME_ATTRIBUTE, endTimeTextView.getText().toString());
					}
					
				}).start();
			}
			
		});
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		updateAvailability();
		
		local_user_information =  this.getSharedPreferences(PREFS_NAME,0);
		local_user_editor = local_user_information.edit();
		local_user_editor.putString("start_time", startTimeTextView.getText().toString());
		local_user_editor.putString("end_time", endTimeTextView.getText().toString());
		local_user_editor.commit();
		
		finish();
	}
	
	@Override
	public void onBackPressed() {
	    super.onBackPressed();
	    overridePendingTransition(R.anim.activity_right_in, R.anim.activity_right_out);
	}
	
	
	private void timeToHourAndMin(){
		String am_or_pm = START_TIME.substring(START_TIME.length()-2, START_TIME.length());
		
		
		String trim_start_time = START_TIME.substring(0, START_TIME.length()-3);
		String trim_end_time = END_TIME.substring(0, END_TIME.length()-3);
		
		start_time_min = Integer.parseInt(trim_start_time.substring(trim_start_time.length()-2, trim_start_time.length()));
		end_time_min = Integer.parseInt( trim_end_time.substring( trim_end_time.length()-2,  trim_end_time.length()));
		
		trim_start_time = trim_start_time.substring(0, trim_start_time.length()-5);
		trim_end_time = trim_end_time.substring(0, trim_end_time.length()-5);
		
		
		start_time_hour = am_or_pm.equals("AM") ? Integer.parseInt(trim_start_time) : Integer.parseInt(trim_start_time) + 12;
		end_time_hour = am_or_pm.equals("AM") ? Integer.parseInt(trim_end_time) : Integer.parseInt(trim_end_time) + 12;
	
		
	/*	start_time_hour = 23;
		end_time_hour = 23;*/
		
	}
	

}
